package server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import database.dao.DoorDAO;
import database.dao.HumidityDAO;
import database.dao.IlluminationDAO;
import database.dao.LightDAO;
import database.dao.MovementDAO;
import database.dao.TemperatureDAO;
import database.manager.DatabaseManager;

public class IPCConnection implements Runnable {

	@Override
	public void run() {
		// Always keep the server up!
		while( true ) {
			try {// connecting with the client
				int port = Integer.parseInt( DatabaseManager.configFile.getProperty( "PORT" ) );
				ServerSocket srvr = new ServerSocket( port );
				Socket skt = srvr.accept();
				System.out.print( "Client connected: " );
				System.out.println( skt.getInetAddress().getHostAddress() );
				BufferedReader in = new BufferedReader( new InputStreamReader( skt.getInputStream() ) );
				PrintWriter out = new PrintWriter( skt.getOutputStream(), true );
				System.out.print( "Stream ready\n" );
				String readline = "";

				try {// readin input from the client
					while( ( readline = in.readLine() ) != null ) {
						String[] temp = readline.split( " " );
						for( String tempValue : temp ) {
							System.out.println( "Received:" + tempValue );
							String sensor = tempValue.substring( 0, 4 );
							String number = tempValue.substring( 4 );
							try {// parsing values retreived from the client
								if( sensor.equals( "DOOR" ) ) {
									Boolean value = false;
									if( number.equals( "1" ) )
										value = true;
									DoorDAO.updateDoor( value );
								} else if( sensor.equals( "HUMI" ) ) {
									Double value = Double.parseDouble( number );
									HumidityDAO.setHumidity( value.intValue() );
								} else if( sensor.equals( "ILLU" ) ) {
									Integer value = Integer.parseInt( number );
									IlluminationDAO.setIllumination( value );
								} else if( sensor.equals( "LIGH" ) ) {
									Boolean value = false;
									if( number.equals( "1" ) )
										value = true;
									LightDAO.setLight( value );
								} else if( sensor.equals( "MOVE" ) ) {
									Boolean value = false;
									if( number.equals( "1" ) )
										value = true;
									MovementDAO.setMovement( value );
									CamConnection.setMovement( value );
								} else if( sensor.equals( "TEMP" ) ) {
									Double value = Double.parseDouble( number );
									TemperatureDAO.setTemperature( value.intValue() );
								} else {
									System.out.println( "Sensortype not recognized" );
								}
							} catch( NumberFormatException e ) {
								System.out.println( "Sensor value not recognized: " + e );
							}
							if( skt.isClosed() )
								break;
						}
					}
				} catch( Exception e ) {
					e.printStackTrace();
					System.out.println( "Connection lost, waiting for new one" );
				}

				out.close();
				in.close();
				skt.close();
				srvr.close();
			} catch( Exception e ) {
				e.printStackTrace();
			}
		}
	}
}
